package com.yksj.healthtalk.ui.doctorstation;

import java.util.ArrayList;
import java.util.List;

import org.handmark.pulltorefresh.library.PullToRefreshBase;
import org.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import org.handmark.pulltorefresh.library.PullToRefreshListView;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.yksj.healthtalk.adapter.DoctorSettingClientAdapter;
import com.yksj.healthtalk.adapter.DoctorSettingClientAdapter.onClickFriendAttentionListener;
import com.yksj.healthtalk.adapter.DoctorSettingClientAdapter.onClickFriendHeadListener;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.CustomerInfoEntity;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.services.MessagePushService;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.friend.FriendInformUI;
import com.yksj.healthtalk.utils.FriendHttpUtil;
import com.yksj.healthtalk.utils.ToastUtil;
/**
 * 咨询客户列表
 * @author Administrator
 *
 */
public class DoctorSettingClientListActivity extends BaseFragmentActivity implements OnClickListener, OnRefreshListener<ListView>, OnItemClickListener, onClickFriendHeadListener, onClickFriendAttentionListener{

	private PullToRefreshListView refreshListView;
	private ListView mListView;
	private List<CustomerInfoEntity> mCustomerInfoEntitys=new ArrayList<CustomerInfoEntity>();
	private DoctorSettingClientAdapter adapter;
	BroadcastReceiver receiver = new BroadcastReceiver(){
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (action.equals(MessagePushService.ACTION_MESSAGE)) {
				if (intent.hasExtra("senderId")) {
					adapter.notifyDataSetChanged();
					}}
		}
	};

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.doctor_client);
		initView();
		initData();
	}

	private void initData() {
		HttpRestClient.doHttpDoctorSettingClient(SmartFoxClient.getLoginUserId(),new AsyncHttpResponseHandler(){

			@Override
			public void onSuccess(int statusCode, String content) {
				super.onSuccess(statusCode, content);
				if(content.contains("error_message")){
					ToastUtil.showBasicShortToast(getApplicationContext(), "服务器异常,请稍后重试");
					return;
				}
				mCustomerInfoEntitys.clear();
				mCustomerInfoEntitys.addAll(FriendHttpUtil.jsonAnalysisFriendEntity( content,true));
				adapter.notifyDataSetChanged();
				if(mCustomerInfoEntitys.size()==0){
					ToastUtil.showToastPanl("暂无数据");
				}
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
				refreshListView.onRefreshComplete();
			}
		});
		
	}

	@Override
	public void onStart() {
		IntentFilter filter = new IntentFilter();
		filter.addAction(MessagePushService.ACTION_MESSAGE);
		registerReceiver(receiver, filter);
		super.onStart();
	}
	
	@Override
	public void onStop() {
		unregisterReceiver(receiver);
		super.onStop();
	}
	
	private void initView() {
		initTitle();
		titleLeftBtn.setOnClickListener(this);
		titleTextV.setText("咨询名单");
		
		titleRightBtn2.setVisibility(View.VISIBLE);
		titleRightBtn2.setText("历史");
		titleRightBtn2.setOnClickListener(this);	
		
		refreshListView = (PullToRefreshListView) findViewById(R.id.listview_fragment);
		mListView = refreshListView.getRefreshableView();
		
		adapter=new DoctorSettingClientAdapter(this,mCustomerInfoEntitys);
		mListView.setAdapter(adapter);
		mListView.setOnItemClickListener(this);
		adapter.setonClickFriendHeadListener(this);
		adapter.setonClickFriendAttentionListener(this);
		refreshListView.setOnRefreshListener(this);
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			overridePendingTransition(R.anim.back_enter,R.anim.back_exit);
			break;
		case R.id.title_right2:
			Intent intent=new Intent(this,DoctorSettingClientHistoryActivity.class);
			startActivity(intent);
			overridePendingTransition(R.anim.anim_enter,R.anim.anim_exit);
			break;
		}
		
	}

	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		initData();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		view.findViewById(R.id.item_dele).setVisibility(View.GONE);
		FriendHttpUtil.onItemClick(this, mCustomerInfoEntitys.get(position-1));
	}

	@Override
	public void onClickFriendHead(CustomerInfoEntity entity, int positon) {
		Intent intent = new Intent();
		intent.setClass(this, FriendInformUI.class);
		intent.putExtra("id", entity.getId());
		startActivity(intent);
	}
/**
 * 查看客户退号
 */
	@Override
	public void onClickFriendAttention(
			CustomerInfoEntity entity, int positon) {
		if("60".equals(entity.getServiceStatusCode())){
			Intent intent=new Intent(this,DoctorSettingExitActivity.class);
			intent.putExtra("id", entity.getId());
			startActivityForResult(intent, 1000);
		}
	}
	
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent data) {
		super.onActivityResult(arg0, arg1, data);
		switch (arg0) {
		case 1000:
			if (data!= null &&data.hasExtra("isTrue")) {
				initData();
			}
			break;
		}
	}

}
